﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Core.Compatibility
{
    public class Compatibility
    {
        public static void UpdateCompatibility(int userId, ref Model.HangoutDBEntities context)
        {
            //delete all the previous computed compatibility :)

            DeleteCompatibilities(userId, ref context);
            System.Diagnostics.Trace.WriteLine("Deleted all the Compatibilities for " + userId);
            //get all the users who are in the radius of 50 Km from that user. 

            if (Web.Core.Location.UserLocation.GetLastLocation(userId, ref context) == null)
            {
                return;
            }

            List<Hangout.Web.Core.Location.UserLocationData> locationData = Core.Location.UserLocation.GetNearestUsers(userId, 50, ref context).ToList();

            List<int> userIds = new List<int>();

            foreach (Hangout.Web.Core.Location.UserLocationData data in locationData)
            {
                userIds.Add(data.UserData.User.UserID);
            }

            //compute compatibility for each and every user :)

            foreach (int friend in userIds)
            {
                //compute the score of this user :)
                int score=0;

                List<Model.UserToken> tokens = context.UserTokens.Where(o => o.UserId == friend).ToList();

                foreach(Model.UserToken t in tokens)
                {
                    Model.UserToken userToken = context.UserTokens.Where(o => o.UserId==userId && o.TokenId==t.TokenId).FirstOrDefault();

                    if (userToken != null)
                    {
                        int scr = Math.Min(t.Score, userToken.Score);
                        score += scr;
                    }
                }

                Model.Compatibility c = new Model.Compatibility();
                c.User1ID = userId;
                c.User2ID = friend;
                c.Score = score;
                c.DateTimeUpdated = DateTime.Now;
                context.AddToCompatibilities(c);
                context.SaveChanges();

            }

        }

        private static void DeleteCompatibilities(int userId, ref  Model.HangoutDBEntities context)
        {
            List<Model.Compatibility> list = context.Compatibilities.Where(o => o.User1ID == userId || o.User2ID == userId).ToList();

            foreach (Model.Compatibility c in list)
            {
                context.Compatibilities.DeleteObject(c);
            }

            context.SaveChanges();
        }

        public static List<int> GetCompatibleUsers(int userid, Model.HangoutDBEntities context)
        {
            List<int> userIds = new List<int>();
            userIds.AddRange(context.Compatibilities.Where(o => o.User1ID == userid).Select(o => o.User2ID).ToList());
            userIds.AddRange(context.Compatibilities.Where(o => o.User2ID == userid).Select(o => o.User1ID).ToList());
            return userIds;
        }


    }
}